package mongo

import (
	"context"
	"dc-news/common/global/setting"
	"fmt"
	"github.com/qiniu/qmgo"
	"github.com/qiniu/qmgo/options"
)

var Cli *qmgo.QmgoClient

func InitMongoDB() (err error) {
	ctx := context.Background()
	Cli, err = qmgo.Open(ctx, &qmgo.Config{
		Uri:      fmt.Sprintf("mongodb://%s:%s", setting.Conf.MongoDBConfig.Host, setting.Conf.MongoDBConfig.Port),
		Database: setting.Conf.MongoDBConfig.Database,
		Coll:     "record",
	})
	_ = Cli.CreateOneIndex(context.Background(), options.IndexModel{Key: []string{"news_id"}, Unique: true})
	return err
}
